﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Serialization;
using System.ServiceModel;
using ResourceLoadBalancer.DataAccess;
using System.Xml;

namespace ResourceLoadBalancer.WebServices.Contracts.Data
{
    [DataContract]
    public class SharePointURIDataContract
    {
        [DataMember]
        public string SiteCollectionURL { get; set; }

        [DataMember]
        public string WebURL { get; set; }

        [DataMember]
        public string DocumentURL { get; set; }

        public XmlDocument ConvertToXmlDocument()
        {
            XmlDocument document = new XmlDocument();// Create the XML Declaration, and append it to XML document
            XmlDeclaration dec = document.CreateXmlDeclaration("1.0", null, null);
            document.AppendChild(dec);// Create the root element
            XmlElement root = document.CreateElement("SharePointResourceURI");
            document.AppendChild(root);

            XmlElement siteCollectionURL = document.CreateElement("SiteCollectionURL");
            siteCollectionURL.InnerText = SiteCollectionURL;
            root.AppendChild(siteCollectionURL);

            XmlElement webURL = document.CreateElement("WebURL");
            webURL.InnerText = WebURL;
            root.AppendChild(webURL);

            XmlElement documentURL = document.CreateElement("DocumentURL");
            documentURL.InnerText = DocumentURL;
            root.AppendChild(documentURL);

            return document;
        }

        public void ConvertFromXmlDocument(XmlDocument document)
        {
            SiteCollectionURL = document.GetElementsByTagName("SiteCollectionURL")[0].InnerText;
            WebURL = document.GetElementsByTagName("WebURL")[0].InnerText;
            DocumentURL = document.GetElementsByTagName("DocumentURL")[0].InnerText;
        }
    }
}
